package com.t.bricks.bussiness.controller.synchtable;

import io.swagger.annotations.Api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.t.bricks.bussiness.db.entity.synchtable.SynchTable;
import com.t.bricks.bussiness.service.synchtable.SynchTableService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.function.data.LongUtil;
import com.t.bricks.utils.function.encode.AESUtil;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 同步表信息控制层
 * @author HuangXinBian
 * @DateTime 2021年08月25日 15:16:19
 */
@RestController
@Api(value="同步表信息", tags={"同步表信息操作接口"})
@RequestMapping("/synch/table/")
public class SynchTableController {

	@Value("${app.key:配置文件中的访问秘钥app.key未设置}")
	private String appKey;

	@Value("${app.token.pass:配置文件中的访问密钥加密用密钥app.token.pass未设置}")
	private String tokenPass;
	
	@Autowired
	private SynchTableService synchTableService;
	
	/**
	 * 判断访问权限
	 * @return
	 */
	private MsgEmity check() {
		HttpServletRequest request = ModuleUtil.currentRequest();
		
		String key = (String)request.getHeader("sToken");//访问密钥
		if (key == null || "".equals(key.trim())) {
			key = request.getParameter("sToken");//请求头没有则从请求参数取
		}
		
		if (null == key || "".equals(key.trim())) {
			return MsgEmity.err(9101, "拒绝请求,请求密钥缺失");
		}
		
		if ("配置文件中的访问密钥加密用密钥app.token.pass未设置".equals(tokenPass)) {
			return MsgEmity.err(9102, "配置文件中的访问密钥加密用密钥app.token.pass未设置");
		}
		
		String temp = AESUtil.decode(key.trim(), tokenPass);
		if (null == temp || "".equals(temp)) {
			return MsgEmity.err(9103, "拒绝请求,请求密钥格式错误");
		}
		
		String[] array = temp.split(";");//sOwner;appkey;timestamp
		if (array.length != 3) {
			return MsgEmity.err(9104, "拒绝请求,请求密钥的格式错误");
		}
		
		if ("配置文件中的访问秘钥app.key未设置".equals(appKey)) {
			return MsgEmity.err(9105, "拒绝请求,请求密钥的格式错误");
		}
		
		if (!appKey.equals(array[1])) {
			return MsgEmity.err(9106, "拒绝请求,请求密钥错误");
		}
		
		if ((System.currentTimeMillis() - LongUtil.toLong(array[2], 0)) > 60000000) {
			return MsgEmity.err(9107, "拒绝请求,请求密钥过期");
		}
		
		return new MsgEmity(true, "请求接受", array[0]);
	}

	/**
	 * 查询当前表最后的时间
	 * @Param sTable
	 * @return MsgEmity
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/max/date")
	public MsgEmity findMaxDate(String sTable) {
		MsgEmity me = check();
		if (!me.isSuccess()) {
			return me;
		}
		
		String sOwner = me.getData();
		
		return synchTableService.findMaxDate(sTable, sOwner);
	}

	/**
	 * 查询当前表最后的时间(格式:yyyy-MM-dd HH:mm:ss)
	 * @Param sTable
	 * @return MsgEmity
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/max/datestr")
	public MsgEmity findMaxDateStr(String sTable) {
		MsgEmity me = check();
		if (!me.isSuccess()) {
			return me;
		}
		
		String sOwner = me.getData();
		
		return synchTableService.findMaxDateStr(sTable, sOwner);
	}

	/**
	 * 获取指定时间内数据
	 * @Param sTable
	 * @param sDateSt
	 * @param sDateEd
	 * @return MsgEmity 返回查询结果
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/find/date")
	public MsgEmity findByDate(String sTable, String sDateSt, String sDateEd) {
		MsgEmity me = check();
		if (!me.isSuccess()) {
			return me;
		}
		
		String sOwner = me.getData();
		
		return synchTableService.findByDate(sTable, sOwner, sDateSt, sDateEd);
	}

	/**
	 * 同步数据
	 * @Param sTable
	 * @param sDateSt
	 * @param sDateEd
	 * @return MsgEmity 返回查询结果
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/data/synch")
	public MsgEmity synch(String sTable, String sDateSt, String sDateEd) {
		MsgEmity me = check();
		if (!me.isSuccess()) {
			return me;
		}
		
		String sOwner = me.getData();
		
		return synchTableService.findByDate(sTable, sOwner, sDateSt, sDateEd);
	}

	/**
	 * 推送数据
	 * @param synchTable
	 * @return MsgEmity
	 */
	@RequestMapping(method={RequestMethod.POST, RequestMethod.GET}, value="/save")
	public MsgEmity save(@RequestBody SynchTable synchTable) {
		MsgEmity me = check();
		if (!me.isSuccess()) {
			return me;
		}
		
		String sOwner = me.getData();
		synchTable.setsOwner(sOwner);//预防
		
		return synchTableService.save(synchTable);
	}
	
	/**
	 * 删除数据
	 * @Param sTable
	 * @Param sDataId
	 * @return MsgEmity
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/del")
	public MsgEmity del(String sTable, String sDataId) {
		MsgEmity me = check();
		if (!me.isSuccess()) {
			return me;
		}
		
		String sOwner = me.getData();
		
		return synchTableService.del(sTable, sDataId, sOwner);
	}
	
	/**
	 * 清理指定用户数据
	 * @return MsgEmity
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/clear")
	public MsgEmity clear() {
		MsgEmity me = check();
		if (!me.isSuccess()) {
			return me;
		}
		
		String sOwner = me.getData();
		
		return synchTableService.clear(sOwner);
	}
	
	/**
	 * 查询各用户最后同步时间
	 * @return MsgEmity
	 */
	@RequestMapping(method={RequestMethod.POST}, value="/find/info")
	public MsgEmity findInfo() {
		MsgEmity me = check();
		if (!me.isSuccess()) {
			return me;
		}
		
		String sOwner = me.getData();
		if (!"huangxinbian".equals(sOwner)) {
			return MsgEmity.err(9001, "拒绝请求,你无权操作");
		}
		
		return synchTableService.findLastDate();
	}
	
}
